<?php
/**
 * @Author:      lyt
 * @DateTime:  2017-09-04 11:13:11
 * @Description: 用于洗车单的控制器
 */

class SceCarWashOrderController extends BaseController
{   
    /**
     * @Author:      lyt
     * @DateTime:  2017-09-04 11:14:42
     * @Description: 洗车单订单面板
     */
    public function washOrderPanel(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'start_time'=>'required',
                "end_time"=>'required',
            )
        );
    
        $response = array('code' => 0, 'msg' => 'ok');
    
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $token=Input::get("token");
            $start_time=Input::get("start_time")." 00:00:00";
            $end_time=Input::get("end_time")." 23:59:59";

            //验证token
            $engineer_data = MaintenanceEngineers::getEngineerByToken($token,$response);
            if(is_null($engineer_data)){
                
                break;
            }

            $finally=array();
            $cwo=CarWashOrder::where("shop_id",$engineer_data->type_id)->where("shop_status",0)->where("created_at",">",$start_time)->where("created_at","<=",$end_time)->where("status",1417);
            if(Input::get("car_no")){
                $cwo=$cwo->where("car_no","like","%".Input::get("car_no")."%");
            }
            $cwo=$cwo->orderBy("pay_time","desc")->select("order_id","car_no","pay_time","status","created_at")->get();
             foreach ($cwo as $_cwo) {
                $finally[]=$_cwo;
            } 

            $cwo=CarWashOrder::where("shop_id",$engineer_data->type_id)->where("shop_status",0)->where("created_at",">",$start_time)->where("created_at","<=",$end_time)->where("status",1411);
            if(Input::get("car_no")){
                $cwo=$cwo->where("car_no","like","%".Input::get("car_no")."%");
            }
            $cwo=$cwo->orderBy("created_at","desc")->select("order_id","car_no","pay_time","status","created_at")->get();
            foreach ($cwo as $_cwo) {
                $finally[]=$_cwo;
            }
                       
            $response = array('code' => 0, 'msg' => 'ok', 'result' => array("count"=>count($finally),"order_arr"=>$finally));
        }while(false);
        Log::info("info", array("system" => "sce" ,"event" => "washOrderPanel", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }


     /**
      * @Author:      lyt
      * @DateTime:  2017-09-04 11:45:35
      * @Description: 洗车单离店
      */
    public function washOrderDeparture(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'order_id'=>'required',
            )
        );
    
        $response = array('code' => 0, 'msg' => 'ok');
    
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $token=Input::get("token");
            $order_id=Input::get("order_id");

            $engineer_data = MaintenanceEngineers::getEngineerByToken($token,$response);
            if(is_null($engineer_data)){
                
                break;
            }
            $orders=CarWashOrder::find($order_id);
            if(is_null($orders)){
                $response = Lang::get("sce_error.order_notexiet_1012");
                break;
            }
            if($orders->shop_id!=$engineer_data->type_id){
                $response = Lang::get("sce_error.order_note_shop_error_1013");
                break;
            }
            if($orders->status != 1417){
                $response = Lang::get("sce_error.order_status_error_1010");
                break;  
            }
            $orders->shop_status=1;
            $orders->save();           
            $result = array(
                'code' => 1,
                'type' => 1,        
                'shop_id' => $engineer_data->type_id,         
                'order_id' => $order_id,
                'plate_car_no' =>"",
                'plate_id' =>"",
            ); 
            RedisHelper::push($result,"anhui_wash_panel");     
            $response = array('code' => 0, 'msg' => 'ok', 'result' => array("order_id"=>$order_id));
        }while(false);
        Log::info("info", array("system" => "sce" ,"event" => "washOrderDeparture", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
    
}